﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

using QuickDev.Common.DB;
using QuickDev.Common.Utils;

using QuickDev.Model;

#endregion

namespace QuickDev.SQLServerDAL
{
    /// <summary>
    /// Data Access Layer For dbo.OPM_ReturnOrderDetail.
    /// </summary>
    public class ReturnOrderDetailDAL
    {
        #region IsExist

        /// <summary>
        /// public static bool IsExist( int returnOrderId , string productNumber )
        /// </summary>
        /// <param name="returnOrderId">returnOrderId</param>
        /// <param name="productNumber">productNumber</param>
        /// <returns>bool Existed</returns>
        public static bool IsExist( int returnOrderId , string productNumber )
        {
            string procedureName = "OPM_ReturnOrderDetail_IsExist";

            SqlParameter[] parameters = {
					new SqlParameter("@ReturnOrderId", SqlDbType.Int,4),
                    new SqlParameter("@ProductNumber", SqlDbType.NVarChar,50),
                    new SqlParameter()
				};
            parameters[0].Value = returnOrderId;
            parameters[1].Value = productNumber;
            parameters[2].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );
                return ( (int)( parameters[2].Value ) == 0 ? false : true );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }

        #endregion

        #region GetModel

        /// <summary>
        /// public ReturnOrderDetail GetModel( int id )
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>ReturnOrderDetail model</returns>
        public static ReturnOrderDetail GetModel( int id )
        {
            string procedureName = "OPM_ReturnOrderDetail_GetModel";

            ReturnOrderDetail model = new ReturnOrderDetail();

            SqlParameter parameter = new SqlParameter( "@Id" , id );

            try
            {
                ReaderFactory.ReaderToModel<ReturnOrderDetail>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName , parameter ) , model );
                return model;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region GetList

        /// <summary>
        /// public List(ReturnOrderDetail) GetList( int returnOrderId, bool showDeleted )
        /// </summary>
        /// <param name="returnOrderId">returnOrderId</param>
        /// <param name="showDeleted">showDeleted</param>
        /// <returns>List(ReturnOrderDetail)</returns>
        public static List<ReturnOrderDetail> GetList( int returnOrderId , bool showDeleted )
        {
            string procedureName = "OPM_ReturnOrderDetail_GetList";

            List<ReturnOrderDetail> modelList = new List<ReturnOrderDetail>();

            SqlParameter[] parameters = {
					new SqlParameter( "@ReturnOrderId", SqlDbType.Int, 4 ),
                    new SqlParameter( "@ShowDeleted", SqlDbType.Bit, 1 )
				};
            parameters[0].Value = returnOrderId;
            parameters[1].Value = showDeleted;

            try
            {
                ReaderFactory.ReaderToList<ReturnOrderDetail>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure , procedureName , parameters ) , modelList );
                return modelList;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Insert

        /// <summary>
        /// public static int Insert( int returnOrderId, string productNumber, int productQty, int returnTypeId , string returnReasonXML , bool otherReasonChecked , string otherReasonContent , int createdBy )
        /// </summary>
        /// <param name="returnOrderId">returnOrderId</param>
        /// <param name="productNumber">productNumber</param>
        /// <param name="productQty">productQty</param>
        /// <param name="returnTypeId">returnTypeId</param>
        /// <param name="returnReasonXML">returnReasonXML</param>
        /// <param name="otherReasonChecked">otherReasonChecked</param>
        /// <param name="otherReasonContent">otherReasonContent</param>
        /// <param name="createBy">createBy</param>
        /// <returns>Id</returns>
        public static int Insert( int returnOrderId , string productNumber , int productQty , int returnTypeId , string returnReasonXML , bool otherReasonChecked , string otherReasonContent , int createdBy )
        {
            if ( IsExist( returnOrderId , productNumber ) )
                throw new ArgumentException
                    ( String.Format( "ReturnOrderDetail {0}-{1} is already existed." , ReturnOrderHeaderDAL.GetModel( returnOrderId ).ReturnOrderNumber , productNumber ) );

            string procedureName = "OPM_ReturnOrderDetail_Insert";

            SqlParameter[] parameters = {
				new SqlParameter("@ReturnOrderId", SqlDbType.Int,4),	
				new SqlParameter("@ProductNumber", SqlDbType.NVarChar,50),	
				new SqlParameter("@ProductQty", SqlDbType.Int,4),	
				new SqlParameter("@ReturnTypeId", SqlDbType.Int,4),	
				new SqlParameter("@ReturnReasonXML", SqlDbType.Xml),	
				new SqlParameter("@OtherReasonChecked", SqlDbType.Bit,1),	
				new SqlParameter("@OtherReasonContent", SqlDbType.NVarChar,4000),	
				new SqlParameter("@CreatedBy", SqlDbType.Int,4),
                new SqlParameter("@Id", SqlDbType.Int,4)
			};

            int nIndex = 0;
            parameters[nIndex++].Value = returnOrderId;
            parameters[nIndex++].Value = productNumber;
            parameters[nIndex++].Value = productQty;
            parameters[nIndex++].Value = returnTypeId;
            parameters[nIndex++].Value = returnReasonXML;
            parameters[nIndex++].Value = otherReasonChecked;
            parameters[nIndex++].Value = otherReasonContent;
            parameters[nIndex++].Value = createdBy;
            parameters[nIndex].Direction = ParameterDirection.Output;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return (int)Utility.ChangeTypeTo( parameters[nIndex].Value , typeof( Int32 ) );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Update

        /// <summary>
        /// public static bool Update( int id, int returnOrderId, string productNumber, int productQty, int returnTypeId, string returnReasonXML, bool otherReasonChecked, string otherReasonContent, int updatedBy )
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="returnOrderId">returnOrderId</param>
        /// <param name="productNumber">productNumber</param>
        /// <param name="productQty">productQty</param>
        /// <param name="returnTypeId">returnTypeId</param>
        /// <param name="returnReasonXML">returnReasonXML</param>
        /// <param name="otherReasonChecked">otherReasonChecked</param>
        /// <param name="otherReasonContent">otherReasonContent</param>
        /// <param name="updatedBy">updatedBy</param>
        /// <returns>bool successful</returns>
        public static bool Update( int id , int returnOrderId , string productNumber , int productQty , int returnTypeId , string returnReasonXML , bool otherReasonChecked , string otherReasonContent , int updatedBy )
        {
            string procedureName = "OPM_ReturnOrderDetail_Update";

            SqlParameter[] parameters = {
				new SqlParameter("@Id", SqlDbType.Int,4),	
				new SqlParameter("@ReturnOrderId", SqlDbType.Int,4),	
				new SqlParameter("@ProductNumber", SqlDbType.NVarChar,50),	
				new SqlParameter("@ProductQty", SqlDbType.Int,4),	
				new SqlParameter("@ReturnTypeId", SqlDbType.Int,4),	
				new SqlParameter("@ReturnReasonXML", SqlDbType.Xml),	
				new SqlParameter("@OtherReasonChecked", SqlDbType.Bit,1),	
				new SqlParameter("@OtherReasonContent", SqlDbType.NVarChar,4000),	
				new SqlParameter("@UpdatedBy", SqlDbType.Int,4),
                new SqlParameter()
			};

            int nIndex = 0;
            parameters[nIndex++].Value = id;
            parameters[nIndex++].Value = returnOrderId;
            parameters[nIndex++].Value = productNumber;
            parameters[nIndex++].Value = productQty;
            parameters[nIndex++].Value = returnTypeId;
            parameters[nIndex++].Value = returnReasonXML;
            parameters[nIndex++].Value = otherReasonChecked;
            parameters[nIndex++].Value = otherReasonContent;
            parameters[nIndex++].Value = updatedBy;
            parameters[nIndex].Direction = ParameterDirection.ReturnValue;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return ( (int)( parameters[nIndex].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion

        #region Delete

        /// <summary>
        /// public bool Delete( int id, int deletedBy )
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="int deletedBy ">int deletedBy </param>
        /// <returns>bool successful</returns>
        public static bool Delete( int id , int deletedBy )
        {
            string procedureName = "OPM_ReturnOrderDetail_Delete";

            SqlParameter[] parameters = {
                new SqlParameter("@Id", SqlDbType.Int, 4),
                new SqlParameter("@DeletedBy", SqlDbType.Int, 4),
                new SqlParameter()
			};

            parameters[0].Value = id;
            parameters[1].Value = deletedBy;
            parameters[2].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure , procedureName , parameters );

                return ( (int)( parameters[2].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database" , ex );
            }
        }
        #endregion
    }
}

